<?php
/**
 * Created by PhpStorm.
 * User: zlkmu
 * Date: 2017/10/7
 * Time: 16:58
 * 用于处理牛牛房间的逻辑
 */
require_once __DIR__ . '/../Object/NiuNiuManager.php';
require_once __DIR__.'/../Context/NiuApi.php';

class NiuNiuRoomLogic{
    private $niuNiuManager;

    function __construct()
    {
        $this->niuNiuManager = new NiuNiuManager();
    }


    //用户加入取牌(得到降序后的牌)
    function quPai(){
        $pai=$this->niuNiuManager->faPai();
        //对得到的牌进行降序排列
        for ($i = 0; $i < 4; $i++) {
            for ($j = $i + 1; $j < 5; $j++) {
                if ($pai[$i]->num < $pai[$j]->num) {
                    $temp = $pai[$i]->num;
                    $pai[$i]->num = $pai[$j]->num;
                    $pai[$j]->num = $temp;
                }
            }
        }
        return $pai;
    }

    //判断用户是否是牛、牛几
    function check($pai){
        $count=0;
        $carDall=0;

        for ($i = 0; $i < 5; $i++) {
            if ($pai[$i]->num >= 10) {
                $count++;
            }

            //得到五张牌的大小
            $carDall += $pai[$i]->num;

//            switch ($pai[$i]->type) {
//                case 1:
//                    echo "黑桃";
//                    break;
//                case 2:
//                    echo "红桃";
//                    break;
//                case 3:
//                    echo "梅花";
//                    break;
//                case 4:
//                    echo "方块";
//                    break;
//            }

//            switch ($pai[$i]->num) {
//                case 11:
//                    echo "J\n";
//                    break;
//                case 12:
//                    echo "Q\n";
//                    break;
//                case 13:
//                    echo "K\n";
//                    break;
//                default:
//                    echo $pai[$i]->num . "\n";
//                    break;
//            }

        }

        //牛牛判断算法
        switch ($count) {
            case 0:
                //都是小于10的牌[需要任意三张牌组成为10的倍数]
                for ($i = 0; $i < 4; $i++) {
                    for ($j = $i + 1; $j < 5; $j++) {
                        if (($carDall - ($pai[$i]->num) - ($pai[$j]->num)) % 10 == 0) {
                            $cow = (($pai[$i]->num) + ($pai[$j]->num)) % 10;
                            return $this->display($cow);
                            //exit(0);
                        }
                    }
                }
                return $this->display(-1);
                break;

            case 1:
                //有一张10以上的[任意两张或三张牌组成为10的倍数]
                for ($i = 1; $i < 4; $i++) {
                    for ($j = $i + 1; $j < 5; $j++) {
                        //任意三张
                        if (($carDall - ($pai[0]->num) - ($pai[$i]->num)) % 10 == 0) {
                            $cow = ($carDall - ($pai[0]->num)) % 10;
                            return $this->display($cow);
                        } //任意两张
                        else if (($carDall - ($pai[0]->num) - ($pai[$i]->num) - ($pai[$j]->num)) % 10 == 0) {
                            $cow = (($pai[$i]->num) + ($pai[$j]->num)) % 10;
                            return $this->display($cow);
                        } //其余情况
                        else if (($carDall - ($pai[0]->num) - ($pai[4]->num)) % 10 == 0) {
                            $cow = ($carDall - ($pai[0]->num)) % 10;
                            return $this->display($cow);
                        }
                    }

                }
                return $this->display(-1);
                break;
            case 2:
                //有两张10以上的[任意两张和为10或者任意三张和为10的倍数]
                for ($i = $count; $i < 4; $i++) {
                    for ($j = $i + 1; $j < 5; $j++) {
                        //计算剩余三张牌的和
                        $temp = $carDall;
                        for ($k = 0; $k < $count; $k++) {
                            $temp -= ($pai[$k]->num);
                        }

                        //当任意两张和为10或三张和为10的倍数
                        if (($pai[$i]->num) + ($pai[$j]->num) == 10 || $temp % 10 == 0) {
                            $cow = $temp % 10;
                            return $this->display($cow);
                        }
                    }
                }
                return $this->display(-1);
                break;
            case 3:
            case 4:
            case 5:
                //三张大于10时，只需要计算剩下两张为什么牛
                for ($i = 0; $i < $count; $i++) {
                    $carDall -= ($pai[$i]->num);
                }
                $cow = $carDall % 10;
                return $this->display($cow);
                break;
        }
    }

    function display($n){
        switch ($n){
            case 1:return NiuAPI::$NIU_ONE;break;
            case 2:return NiuAPI::$NIU_TWO;break;
            case 3:return NiuAPI::$NIU_THREE;break;
            case 4:return NiuAPI::$NIU_FOUR;break;
            case 5:return NiuAPI::$NIU_FIVE;break;
            case 6:return NiuAPI::$NIU_SEX;break;
            case 7:return NiuAPI::$NIU_SEVEN;break;
            case 8:return NiuAPI::$NIU_EIGHT;break;
            case 9:return NiuAPI::$NIU_NINE;break;
            case 0:return NiuAPI::$NIU_NIU;break;
            case -1:return NiuAPI::$NOTHING;break;
        }
    }

}



?>